package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.domain.User;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //忽略字符串里的空格
        if (queryString != null && queryString != "") {
            queryString = queryString.replaceAll(" ","");
        }
        PageHelper.startPage(currentPage,pageSize);
        Page page = addressDao.findCondition(queryString);
        long total = page.getTotal();
        List result = page.getResult();

        return new PageResult(total,result);
    }

    @Override
    public void delete(Integer id) {
        addressDao.delete(id);
    }

    @Override
    public void add(Map map) {
        addressDao.add(map);
    }
}
